package com.sinosoft.cnmuseum.servlets;

import com.sinosoft.cnmuseum.dao.MbXtglDhxxDao;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.record.Record;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @Author: liangxiao
 * @Date: Created in 14:13 2019/1/23
 */
public class AccessControlServlet {
    @rquestMethod(MethodNote = rquestMethod.ReqMode.All)
    public Record getAccessBar(HttpServletRequest request, HttpServletResponse response, Record data) {
        Record record = new Record();
        record.add(Status.SATUS, Status.OK);
        try {
            HttpSession session = request.getSession(false);
            String userid = (String) session.getAttribute(Status.SESSION_USERID);
            //从人员角色表中
            String sql = "SELECT * FROM mb_xtgl_dhxx WHERE isview=1 or zdbh IN (SELECT DISTINCT dhbm FROM mb_xtgl_jsdhgl WHERE jsbm in (SELECT jsbm FROM mb_xtgl_ryjsfp WHERE rybm=" + userid + "))";
            MbXtglDhxxDao mbXtglDhxxDao = new MbXtglDhxxDao();
            List<Record> list = mbXtglDhxxDao.getBySql(sql);
            // 组装数据
            Map<String, List<String>> accessMap = new TreeMap<>();
            if (list != null && list.size() > 0) {
                for (Record r : list) {
                    //一级标题
                    String yjbt = r.getStrValue("yjbt");
                    //二级标题
                    String ejbt = r.getStrValue("ejbt");
                    //url
                    String url = r.getStrValue("url");
                    //imgpath
                    String imgpath = r.getStrValue("imgpath");
                    if (accessMap.containsKey(yjbt)) {
                        accessMap.get(yjbt).add(ejbt + "####" + url + "####" + imgpath);
                    } else {
                        List<String> ejbtList = new ArrayList<>();
                        ejbtList.add(ejbt + "####" + url + "####" + imgpath);
                        accessMap.put(yjbt, ejbtList);
                    }
                }
            }

            List<Record> resultList = new ArrayList<>();
            Iterator iter = accessMap.entrySet().iterator();
            while (iter.hasNext()) {
                Record record1 = new Record();
                Map.Entry entry = (Map.Entry) iter.next();
                // 获取key,一级标题
                String key = (String) entry.getKey();
                record1.add("menuName", key);
                // 获取value
                List<String> value = (List<String>) entry.getValue();
                List<Record> menuList = new ArrayList<>();
                for (String v : value) {
                    Record singleMap = new Record();
                    String[] subStr = v.split("####");
                    String ejbt = v.split("####")[0];
                    if (subStr.length > 1) {
                        //String url = v.split("####")[1];
                        singleMap.add("url", subStr[1]);
                        singleMap.add("imgpath", subStr[2]);
                        singleMap.add("name", subStr[0]);
                        menuList.add(singleMap);
                    } else {
                        //singleMap.add("url", "#");
                    }
                }
                record1.add("menuList", menuList);
                resultList.add(record1);
            }
            record.add("menu", resultList);

        } catch (Exception e) {
            record.add(Status.SATUS, Status.FAILED);
            record.add(Status.MSG, "数据异常！");
        }
        return record;
    }
}
